﻿package MyComponent// ActionScript file
{
	import fl.core.InvalidationType;
	import fl.core.UIComponent;
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;

	public class MyButton extends UIComponent
	{
		private var _background:DisplayObject;
		private var _mouseState:String;
		private var _skinsType:String="default";
		private static var defaultStyles:Object={upSkin:"MyButton_default_upSkin",downSkin:"MyButton_default_downSkin",overSkin:"MyButton_default_overSkin",disabledSkin:"MyButton_default_disabledSkin"};
		private var _downSign:Boolean=false;
		private var _lockState:Boolean=false;
		public function MyButton():void
		{
			super();
			this.addListener();
			this.setMouseState("up");
		}
		
		public static function getStyleDefinition():Object
		{
			return defaultStyles;
		}
		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler,false,0,true);
			this.addEventListener(MouseEvent.ROLL_OVER,mouseEventHandler,false,0,true);
			this.addEventListener(MouseEvent.ROLL_OUT,mouseEventHandler,false,0,true);
		}
		
		private function mouseEventHandler(e:MouseEvent):void
		{
			if(!this.enabled)
			{
				return;
			}
			if (e.type == MouseEvent.MOUSE_DOWN) 
			{
				setMouseState("down");
				this.dispatchEvent(new ComponentEvent(ComponentEvent.BUTTON_DOWN,true));
				this.downSign=true;
			}
			else if (e.type == MouseEvent.ROLL_OVER)
			{
				setMouseState("over");
			} 
			else if (e.type == MouseEvent.ROLL_OUT)
			{
				if(!this.lockState)
				{
				    setMouseState("up");
				}
				else
				{
					setMouseState("over");
				}
				this.downSign=false;
			}
			else if(e.type == MouseEvent.MOUSE_UP)
			{
				setMouseState("over");
				if(this.downSign)
				{
					this.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
					this.downSign=false;
				}
			}
		}
		
		override protected function draw():void
		{
			if (this.isInvalid(InvalidationType.STYLES,InvalidationType.STATE))
			{
				this.drawBackground();
				this.invalidate(InvalidationType.SIZE,false); 
			}
			if (this.isInvalid(InvalidationType.SIZE)) {
				this.drawLayout();
			}
			super.draw();
		}
		
		protected function drawBackground():void
		{
			var styleName:String=this.enabled?mouseState:"disabled";
			styleName+="Skin";
			if(this.background)this.removeChild(this.background);
			this.background=this.getDisplayObjectInstance(this.getStyleValue(styleName));
			if(this.background)this.addChild(this.background);
		}
		
		protected function drawLayout():void
		{
			this.background.width=this.width;
			this.background.height=this.height;
		}
		
		public function setMouseState(value:String):void
		{
			if(this.mouseState==value)
			{
				return;
			}
			this.mouseState=value;
			this.invalidate(InvalidationType.STATE);
		}
		
		
		public function release():void
		{
			
		}

		/*getter & setter*/
		public function get background():DisplayObject
		{
			return _background;
		}

		public function set background(value:DisplayObject):void
		{
			_background = value;
		}

		public function get mouseState():String
		{
			return _mouseState;
		}

		public function set mouseState(value:String):void
		{
			_mouseState = value;
		}
		
		[Inspectable(defaultValue=true)]
		override public function get enabled():Boolean
		{
			return super.enabled;
		}
		
		override public function set enabled(arg0:Boolean):void
		{
			super.enabled=arg0;
			this.mouseEnabled=arg0;
		}
		
		[Inspectable(defaultValue="MyButton_default",type="String")]
		public function set skinsType(value:String):void
		{
			if(value==this.skinsType){return;}
			var upSkinStr:String=value+"_upSkin";
			var downSkinStr:String=value+"_downSkin";
			var overSkinStr:String=value+"_overSkin";
			var disabledSkinStr:String=value+"_disabledSkin";
			try
			{
				var upSkinClass:Object=getDefinitionByName(upSkinStr);
				var downSkinClass:Object=getDefinitionByName(downSkinStr);
				var overSkinClass:Object=getDefinitionByName(overSkinStr);
				var disabledSkinClass:Object=getDefinitionByName(disabledSkinStr);
				this.setStyle("upSkin",upSkinStr);
				this.setStyle("downSkin",downSkinStr);
				this.setStyle("overSkin",overSkinStr);
				this.setStyle("disabledSkin",disabledSkinStr);
				_skinsType = value;
			}
			catch(e:Error)
			{
				trace("没有资源");
			}
		}
		
		public function get skinsType():String
		{
			return _skinsType;
		}

		public function get downSign():Boolean
		{
			return _downSign;
		}

		public function set downSign(value:Boolean):void
		{
			_downSign = value;
		}

		public function get lockState():Boolean
		{
			return _lockState;
		}

		public function set lockState(value:Boolean):void
		{
			_lockState = value;
			if(this.lockState)
			{
				setMouseState("over");
			}
			else
			{
				setMouseState("up");
			}
		}


	}
}